﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LS.Cores
{
    /// <summary>
    /// 企业统计信息
    /// </summary>
    public class CmsStatisticsService : ICmsStatistics
    {
        public DataTable GetCompanyVisitTop(int top)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append($" select top {top} companyName,ReadCount From cms_company order by ReadCount desc ");
            return SqlHelper.QueryDataTable(sql.ToString());
        }

        public DataTable GetPrductUpdateOrder(int top,string userId)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendLine(" select  * From ( ");
            sql.Append($" select  top {top} title, (case when UpdateDate is not null then CONVERT(varchar(10), UpdateDate,120)  else CONVERT(varchar(10), createdate,120)  end )  updatetime  ");
            sql.Append($" From cms_company_product where isdel=0 and FlowStatus=2  and  CompanyID in(select id From cms_company where isdel=0 and FlowStatus=2 and CompStatus=1 and createby='{userId}') order by UpdateDate desc ,createdate desc   ");
            sql.AppendLine(" )  as a order by updatetime desc ");
            return SqlHelper.QueryDataTable(sql.ToString());
        }

       
        public DataTable GetPrductVisitCompanyTop(int top,string userId)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append($" select top {top} title,ReadCount From cms_company_product where isdel=0 and  CompanyID in(select id From cms_company where isdel=0 and FlowStatus=2 and CompStatus=1 and createby='{userId}')  order by ReadCount desc ");
            return SqlHelper.QueryDataTable(sql.ToString());
        }

        public DataTable GetPrductVisitTop(int top)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append($" select top {top} title,ReadCount From cms_company_product where isdel=0 order by ReadCount desc ");
            return SqlHelper.QueryDataTable(sql.ToString());
        }

        public DataTable GetSettled()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(" select ");
            sql.Append(" (select count(1) From cms_company where isdel=0 and FlowStatus=2 and CompStatus=1) as tcompany, ");
            sql.Append(" (select count(1) From cms_company where isdel=0 and FlowStatus=2 and CompStatus=1 and DATEDIFF(M,createdate,GETDATE())=0 ) as tmcompany, ");
            sql.Append(" (select count(1) from  cms_company_product where isdel=0 and FlowStatus=2 )as tproduct, ");
            sql.Append(" (select count(1) From cms_company_product where isdel=0 and FlowStatus=2  and DATEDIFF(M,createdate,GETDATE())=0 )as tmproduct ");
            return SqlHelper.QueryDataTable(sql.ToString());
        }

        public DataTable GetTraffic()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(" select ");
            sql.Append(" (select count(1) From  cms_read_logs where DATEDIFF(d,createdate,getdate())=0) as today, ");
            sql.Append(" (select count(1) From  cms_read_logs where DATEDIFF(d,createdate,getdate())=1 ) as yesterday, ");
            sql.Append(" (select count(1) From  cms_read_logs where DATEDIFF(d,createdate,getdate())=2 )as beforeyesterday ");
            return SqlHelper.QueryDataTable(sql.ToString());
        }

        public DataTable GetVisitLineWeek(string starttime,string endtime)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append($" select * From cms_read_logs where createdate>'{starttime}' and createdate<DATEADD(d,1,'{endtime}')  ");
            return SqlHelper.QueryDataTable(sql.ToString());
        }
    }
}
